package com.mmd.base.pojo.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;
import java.io.Serializable;
import java.util.Date;
import java.util.Optional;
import java.util.OptionalInt;

@Data
@ApiModel("所有实体类的超类")
public class BaseEntity<T> implements Serializable {
    private static final long serialVersionUID = 5555293951111733811L;

    @ApiModelProperty("唯一主键id")
    @TableId(type = IdType.AUTO)
    private Long id;

    @ApiModelProperty("物理删除")
    @JsonIgnore
    @TableLogic
    private Integer isDel = 0;

    @ApiModelProperty("当前数据 备注系统级字段 非业务逻辑字段")
    private String remark = "";

    @ApiModelProperty("更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @ApiModelProperty("创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    public String getRemark() {
        String remark = Optional.ofNullable(this.remark).orElse("");
        if (StringUtils.isEmpty(remark)) {
            return "系统生成:";
        }
        if (remark.contains("系统生成:")) {
            return remark;
        }
        return this.remark;
    }

    public Integer getIsDel() {
        return OptionalInt.of(isDel).orElse(0);
    }
}
